package edu.hawaii.halealohacli.kmj;

import javax.xml.datatype.DatatypeConfigurationException;
import org.wattdepot.client.WattDepotClient;
import java.util.Scanner;

/**
 * The main class that will be the entry point from the final jar.
 * 
 * @author Micah
 *
 */
public class HaleAlohaCli {
  
  static Scanner scanner = new Scanner(System.in);
  static String command = "";
  /**
   * Main method. Parses user input, queries WattDepot for requested data, and then prints results
   * to command line. Runs in an infinite loop until user quits.
   * 
   * @param args Command line input. Made up of a valid command followed by appropriate parameters.
   * 
   * Valid commands:
   *    quit, help, current-power, daily-energy, energy-since, rank-towers
   *    
   * Valid parameters:
   *    quit:           None.
   *    help:           None.
   *    current-power:  Name of entity to check (Ilima-A, Mokihana, etc).
   *    daily-energy:   Name of entity followed by date in yyyy-mm-dd format (Mokihana 2011-11-05).
   *    energy-since:   Name of entity followed by date in yyyy-mm-dd format (Mokihana 2011-11-05).
   *    rank-towers:    Start date and end date in yyyy-mm-dd format (2011-11-05 2011-13-05).
   * @throws DatatypeConfigurationException 
   */
  public static void main(String[] args) throws DatatypeConfigurationException {
    
    // Create client and connect to server.
    WattDepotClient client = new WattDepotClient("http://server.wattdepot.org:8190/wattdepot/");
    CommandParser parser = new CommandParser(client);
    
    // Check that connection was successful. Else, exit.
    if (client.isHealthy()) {
      System.out.println("Connected successfully to the Hale Aloha WattDepot server.");
    }
    else {
      System.out.println("Error: Could not connect to WattDepot server." +
          "Server may be down, or there may not be an internet connection.");
      System.exit(1);
    }

    // Main loop. Continue waiting for commands until user exits.
    boolean done = false;
    while (!done) {
      System.out.print("\nEnter in a command:");
      command = scanner.nextLine();
      if ("quit".equals(command)) {
        done = true;
      } 
      else {
        parser.commandCheck(command);
      }
    }
    
  }
}
